<template>
  <div class="container">
    <el-card>
      <!-- 搜索 -->
      <div class="left">
        <el-form :inline="true" :model="requerstParams" class="demo-form-inline">
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="requerstParams.directoryName" />
          </el-form-item>
          <el-form-item class="zhuangtai" label="状态" prop="state">
            <el-select v-model="requerstParams.state">
              <el-option label="启用" :value="1" />
              <el-option label="禁用" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetForm">清除</el-button>
          </el-form-item>
          <el-form-item class="search">
            <el-button type="primary" @click="handleFilter">搜索</el-button>
          </el-form-item>
          <el-form-item style="float:right">
            <el-button type="success" @click="dialogVisible=true">
              <i class="el-icon-edit" />
              <span>添加目录</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 目录记录 -->
      <template>
        <el-alert v-if="alertText !== ''" :title="alertText" type="info" class="alert" :closable="false" show-icon />
      </template>
      <!-- 数据列表 -->
      <template>
        <el-table style="width: 100%" :data="dirList">
          <el-table-column align="center" label="序号" type="index" />
          <el-table-column label="所属学科" align="center" prop="subjectName" />
          <el-table-column label="目录名称" align="center" prop="directoryName" />
          <el-table-column label="创建者" align="center" prop="creatorID" />
          <el-table-column label="创建日期" align="center" prop="addDate">
            <template slot-scope="scope">
              <span>{{ subTime(scope.row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="面试题数量" align="center" prop="totals" />
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <!-- <span v-if="scope.row.state==1">启用</span>
              <span v-else>禁用</span> -->
              <span>{{ formState(scope.row.state) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="handleStatus(scope.row)">
                <span v-if="scope.row.state==0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button type="text" size="small" :disabled="!(scope.row.state === 0)" @click="handleChange(scope.row)">修改</el-button>
              <el-dialog title="修改目录" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" @open="openList">
                <el-form ref="datafrom" v-model="modificar" status-icon :rules="rules" label-width="7.5rem">
                  <el-form-item label="所属学科" prop="subjectName">
                    <el-select v-model="choiceName" placeholder="请选择">
                      <el-option v-for="obj in listArr" :key="obj.value" :value="obj.label" />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="目录名称" placeholder="请输入目录名称">
                    <el-input v-model="dirName" />
                  </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="handleClose">取 消</el-button>
                  <el-button type="primary" @click="SubmitName">确 定</el-button>
                </span>
              </el-dialog>

              <el-button v-if="scope.row.status!='deleted'" type="text" size="small" :disabled="!(scope.row.state === 0)" @click="removeUser(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <template>
          <el-pagination :current-page="requerstParams.page" background :total="total" :page-sizes="[5, 10, 20, 50]" :page-size="requerstParams.pagesize" layout="sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="changePage" />
        </template>
      </template>
      <directorys-add />
    </el-card>
    <!-- 添加目录弹窗 -->
    <el-dialog title="新增目录" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" @open="openList">
      <el-form ref="datafrom" status-icon :model="directory" :rules="rules" label-width="7.5rem">
        <el-form-item label="所属学科" prop="subjectName">
          <el-select v-model="directory.subjectName" placeholder="请选择">
            <el-option v-for="obj in listArr" :key="obj.value" :value="obj.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName" placeholder="请输入目录名称">
          <el-input v-model="directory.directoryName " />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>

        <el-button type="primary" @click="newdirectory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// import Tpyes from '@/api/hmmm/constants'
import directorysAdd from '../components/directorys-add.vue'
import { simple } from '@/api/hmmm/subjects'
import { list, add, remove, update } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
import moment from 'moment'
export default {
  name: 'Directorys',
  components: { directorysAdd },
  data () {
    return {
      startTtag: true,
      status,
      // 状态
      modificar: '',
      choiceName: '',
      dirName: '',

      listArr: [],
      alertText: '',
      // 新增目录输入框
      directory: {
        directoryName: '',
        state: ''
      },
      // 添加弹窗
      dialogVisible: false,
      // form表单数据
      formDate: {
        subjectName: '',
        directoryName: '',
        creator: '',
        addDate: '',
        totals: '',
        state: ''
      },
      // formInline: {
      //   user: '',
      //   region: ''
      // },
      // 表单验证
      rules: {
        subjectName: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '所属学科不能为空', trigger: 'change' }
        ]
      },
      // 列表总数
      total: 0,
      // 目录列表
      dirList: [],
      // 查询参数
      requerstParams: {
        page: 1,
        pagesize: 10

      }
    }
  },
  // computed: {
  //   status () {
  //     return status
  //   }
  // },
  created () {
    this.grtList()
  },
  methods: {
    formState (state) {
      // console.log(status)
      const info = this.status.find(item => Number(item.value) === state)
      if (info) {
        return info.label
      } else {
        return ''
      }
    },
    handleStatus (val) {
      if (val.state === 0) {
        val.state = 1
        // this.status = '禁用'
      } else {
        val.state = 0
        // this.startTtag = true
        // this.status = '启用'
      }
      this.$message.success('操作成功')
    },
    // 弹窗关闭
    handleClose () {
      this.dialogVisible = false
      this.$refs.datafrom.resetFields()
    },
    // 修改
    handleChange (item) {
      // 显示弹窗, 获取id
      this.dialogVisible = true
      console.log(item)
      this.modificar = item.id
      this.choiceName = item.subjectName
      this.dirName = item.directoryName
    },
    SubmitName () {
      this.dialogVisible = false
      update({ id: Number(this.modificar), subjectName: this.choiceName, directoryName: this.dirName }).then(data => {
        if (data.status === 200) {
          this.grtList()
        }
      })
      this.choiceName = ''
    },
    // 清除
    resetForm () {
      this.formInline.user = this.requerstParams.state = ''
    },
    // 删除
    removeUser (user) {
      this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: user })
            .then(response => {
              this.$message.success('成功删除了用户' + '!')
              this.grtList()
            })
            .catch(response => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // 添加目录
    // newdirectory () {
    //   this.$refs.datafrom.validate(async valid => {
    //     if (!valid) return
    //     // 验证通过
    //     try {
    //       const ret = await add(this.directory)
    //       console.log(ret)
    //       if (ret.code === 200) {
    //         // 添加成功: 关闭弹窗, 刷新列表, 清空表单
    //         this.dialogVisible = false
    //         this.grtList()
    //         this.$refs.datafrom.resetFields()
    //       }
    //     } catch {
    //       this.$message.error('添加目录失败')
    //     }
    //   })
    // },
    // 获取到所属学科
    async openList () {
      const ret = await simple(this.directory)
      // console.log(ret)
      this.listArr = ret.data
    },
    // 新增目录提交
    newdirectory () {
      this.dialogVisible = false
      add({ subjectID: Number(this.directory.subjectID), directoryName: this.directory.directoryName }).then(data => {
        // console.log(data)
        if (data.status === 200) {
          this.grtList()
          this.handleClose()
        }
      })
    },
    // 格式化时间
    subTime (row) {
      return moment(row).format('YYYY-MM-DD HH:mm:ss')
    },

    // 监听页码的变化
    changePage (page) {
      this.requerstParams.page = page
      this.grtList()
    },
    handleSizeChange (page) {
      this.requerstParams.pagesize = page
      this.grtList()
    },
    // 搜索信息
    async handleFilter () {
      const ret = await list(this.requerstParams)
      // console.log(ret)
      this.dirList = ret.data.items
    },
    // // 初始数据
    // initialDate () {
    //   // 读取数据
    //   this.getList()
    // },
    // 获取列表数据
    async grtList () {
      const res = await list(this.requerstParams)
      console.log(res)
      // 获取数据给list
      this.dirList = res.data.items
      // 总数
      this.total = res.data.counts
      this.alertText = `共 ${this.total} 条记录`
    }
  }
}
</script>

<style scopet lang='less'>
.cntainer-left {
  .el-form-item {
    text-align: right;
    vertical-align: middle;
    float: left;
    font-size: 0.875rem;
    color: #606266;
    line-height: 2rem;
    padding: 0 0.75rem 0 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  .zhuangtai {
    margin-left: 25px;
  }
  .search {
    margin-left: 8px;
  }
  .catalogue {
    color: #fff;
    background-color: #67c23a;
  }
}
</style>
